const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stage_array: [],
    stageName: '',
    showModal: false,
    showEditModal: false,
    stageId: '',
    stageNameNew: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let _that = this;
    app.getToken().then(function (){
      _that.loadData();
    });
  },

  loadData: function (){
    const thz = this;
    wx.showLoading({
      title: '加载中'
    });
    wx.request({
      url: app.globalData.requestUrl + 'api/stage/lists',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
      },
      success: function (ret) {
        wx.hideLoading();
        console.log(ret.data);
        var data = ret.data;
        thz.setData(
          {
            stage_array: data.data.lists
          }
        );
      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //创建阶段
  /**
     * 弹窗
     */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  showEditDialogBtn: function (e) {
    var tid = e.currentTarget.dataset.tid;
    var tname = e.currentTarget.dataset.tname;
    this.setData({
      showEditModal: true,
      stageId: tid,
      stageNameNew: tname
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  hideEditModal: function () {
    this.setData({
      showEditModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  onEditCancel: function () {
    this.hideEditModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var thz = this;
    if (this.data.stageName == '') {
      wx.showToast({
        title: '请输入阶段名称',
        icon: 'none',
      });
      return;
    }
    wx.request({
      url: app.globalData.requestUrl + 'api/stage/create',
      method: 'POST',
      data: { stage_name: this.data.stageName },
      success: function (ret) {
        var status = ret.status;
        var data = ret.data;
        console.log(data);
        thz.setData(
          {
            stage_array: data.data.stages
          }
        );
        //成功提示
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        })

      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
    this.hideModal();
    thz.storeStage();
  },
  onEditConfirm: function () {
    var thz = this;
    if (this.data.stageNameNew == '') {
      wx.showToast({
        title: '请输入阶段名称',
        icon: 'none',
      });
      return;
    }
    wx.request({
      url: app.globalData.requestUrl + 'api/stage/edit',
      method: 'POST',
      data: { id:this.data.stageId, name: this.data.stageNameNew },
      success: function (ret) {
        var status = ret.status;
        var data = ret.data;
        console.log(data);
        thz.setData(
          {
            stage_array: data.data.stages
          }
        );
        //成功提示
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        })
        thz.storeStage();
        thz.onLoad();
      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
    this.hideEditModal();
  },
  inputChange: function (e) {
    this.setData(
      {
        stageName: e.detail.value
      }
    );
  },
  inputEditChange: function (e) {
    this.setData(
      {
        stageNameNew: e.detail.value
      }
    );
  },
  deleteStage: function (e) {
    var thz = this;
    var tid = e.currentTarget.dataset.tid;

    wx.showModal({
      title: '提示',
      content: '是否删除阶段？',
      success: function (res) {
        if (res.cancel) {
          return;
        }
        wx.request({
          url: app.globalData.requestUrl + 'api/stage/delete',
          method: 'POST',
          data: { id: tid },
          success: function (ret) {
            
            var data = ret.data;
            console.log(data);
            if (data.status !== 0){
              thz.setData(
                {
                  stage_array: data.data.stages
                }
              );
            }else{
              wx.showToast({
                title: data.msg,
                icon: 'none',
                duration: 1000,
                mask: true
              })
            }
          },
          fail: function (e) {
            wx.showLoading({
              title: '系统错误请稍后重试',
            });
          }
        }); //end delete
        thz.storeStage();
      }
    });
  },
  storeStage:function(){
    wx.request({
      url: app.globalData.requestUrl + 'api/stage/lists',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
      },
      success: function (res) {

        wx.setStorageSync('stages', res.data.data.lists);

      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
  }

})